/*
 * BestChartEntry.java
 *
 * Created on June 27, 2006, 2:09 PM
 *
 */

package galronnlp.pcfg.parser;

import galronnlp.pcfg.parser.EntryTriple;
import galronnlp.util.SortedLinkedList;
import galronnlp.util.Tuple;

import java.util.LinkedList;
import java.util.Iterator;

/**
 * A class that keeps track of the best probability, updating itself
 * when higher probabilities arise.
 *
 * <p>
 * This is based on the Chart Entries as defined in Chris Brew's python
 * Statistical CKY parser
 *
 * @author Daniel A. Galron
 */
public class BestChartEntry implements ChartEntry {
    
    double probability = 0.0;
    int count = 0;
    SortedLinkedList<EntryTriple> items = new SortedLinkedList<EntryTriple>();
    
    /** Creates a new instance of BestChartEntry */
    public BestChartEntry() {
        items.addLast(new EntryTriple());
    }

    /**
     * This method checks to see if the probability of the entry is greater
     * than the probability stored in the chart entry. If it is, this method
     * replaces it with the new entry.
     */
    public void add(EntryTriple entry) {
        double entryProb  = ((Double)entry.get(0)).doubleValue();
        int entryCount = ((Integer)entry.get(1)).intValue();
        if(entryProb > this.probability) {
            items.remove();
            items.add(entry);
            this.probability = entryProb;
        }
    }
    
    public int count() {
        return count;
    }
    
    public double probability() {
        return probability;
    }
    
    public SortedLinkedList<EntryTriple> items() {
        return items;
    }
    
    public String toString() {
        String ret = "(" + this.probability + ", " + this.count + ", {";
        for(Iterator<EntryTriple> i = items.iterator(); i.hasNext(); ) {
            ret += i.next() + ", ";
        }
        ret += "})\n";
        return ret;
    }    
}
